﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StatePattern
{
    //环境类
    public class MyContext
    {
        private State state;
        //定义环境类的初始状态
        public MyContext()
        {
            this.state = new ConcreteStateA();
        }
        //设置新状态
        public void setState(State state)
        {
            this.state = state;
        }
        //读取状态
        public State getState()
        {
            return (state);
        }
        //对请求做处理
        public void Handle()
        {
            state.Handle(this);
        }
    }
    //抽象状态类
    public abstract class State
    {
        public abstract void Handle(MyContext context);
    }
    //具体状态A类
    public class ConcreteStateA : State
    {
        public override void Handle(MyContext context)
        {
            Console.WriteLine("当前状态是 A.");
            context.setState(new ConcreteStateB());
        }
    }
    //具体状态B类
    public class ConcreteStateB : State
    {
        public override void Handle(MyContext context)
        {
            Console.WriteLine("当前状态是 B.");
            context.setState(new ConcreteStateA());
        }
    }
}
